package com.example.rpsm_property.mapper;

import com.example.rpsm_property.pojo.Pay;
import com.example.rpsm_property.pojo.PayInfo;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;

@Mapper
public interface PayMapper {
    @Select("<script>" +
            "select * from pay where pay_id = #{id}" +
            "</script>")
    Pay queryById(Integer id);




    @Select("<script>" +
            "select " +
            "    p.pay_id, " +
            "    b.building_id, " +
            "    r.room_id, " +
            "    u.userid " +
            "from pay p " +
            "inner join room r on p.pay_room_id = r.room_id " +
            "inner join floor f on r.room_floor_id = f.floor_id " +
            "inner join building b on f.belong_building = b.building_id " +
            "left join `user` u on u.user_roomid = r.room_id " +
            "<where> " +
            "    <if test='username != null and username != \"\" or idCard != null and idCard != \"\" or start != null and end != null or floorId != null or buildingId != null'> " +
            "        <if test='username != null and username != \"\"'> " +
            "            and u.username like concat('%', #{username}, '%') " +
            "        </if> " +
            "        <if test='idCard != null and idCard != \"\"'> " +
            "            and u.idcard like concat('%', #{idCard}, '%') " +
            "        </if> " +
            "        <if test='start != null and end != null'> " +
            "            and p.pay_date BETWEEN #{start} and #{end} " +
            "        </if> " +
            "        <if test='floorId != null'> " +
            "            and r.room_floor_id = #{floorId} " +
            "        </if> " +
            "        <if test='buildingId != null and floorId == null'> " +
            "            and f.belong_building = #{buildingId} " +
            "        </if> " +
            "    </if> " +
            "</where> " +
            "</script>")

    @Results(id="payInfo",value = {
            @Result(property = "user",column = "userid",one =@One(select =
                    "com.gzy.rspm.mapper.RegisterMapper.queryById")),
            @Result(property = "building",column = "building_id",one =
            @One(select="com.gzy.rspm.mapper.BuildingMapper.queryById")),
            @Result(property = "room",column = "room_id",one =
            @One(select="com.gzy.rspm.mapper.RoomMapper.queryById")
            ), @Result(property = "pay",column = "pay_id",one =
    @One(select="com.gzy.rspm.mapper.PayMapper.queryById")),})
    Page<PayInfo> selectPayInfoOnCondition(
            @Param("start") LocalDate start,
            @Param("end") LocalDate end,
            @Param("username") String username,
            @Param("idCard") String idCard,
            @Param("floorId") Integer floorId,
            @Param("buildingId") Integer buildingId
    );

}